﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Newtonsoft.Json;
using Meeting.Model;
using Components;
using System.Configuration;

namespace Meeting.Web.ashx
{
    public partial class SaveTopicPlate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string header = "{0}";
            try
            {
                //首先
                bool base64 = Convert.ToBoolean(Request["base64"]);
                Guid topicId = new Guid(Request["topicId"]);
                if (base64)
                {
                    //首先保存文件
                    HttpPostedFile postedFile = Request.Files[0];
                    string uploadPath = Server.MapPath("~/" + ConfigurationManager.AppSettings["UploadPath"] + "/plate");
                    if (postedFile != null)
                    {
                        Guid plateId = Guid.NewGuid();

                        string fullName = postedFile.FileName;
                        //获取文件后缀名
                        string extension = fullName.Substring(fullName.LastIndexOf("."), (fullName.Length - fullName.LastIndexOf("."))).ToLower();
                        //按年月生成文件夹
                        string dateFolder = StrHelp.MakeFolderName();
                        //拼接上传路径
                        uploadPath = uploadPath + "/" +  dateFolder + "/" + topicId.ToString();
                        if (!Directory.Exists(uploadPath))
                        {
                            Directory.CreateDirectory(uploadPath);
                        }
                        string filePath = uploadPath + "/" + plateId + extension;
                        postedFile.SaveAs(filePath);
                        //文件保存成功之后，在数据库中插入一条记录
                        TopicPlate model = new TopicPlate();
                        model.Comment = "";
                        model.IsActive = true;
                        model.Id = plateId;
                        model.Name = fullName.Replace(extension, "");
                        model.TopicId = topicId;
                        model.Path = dateFolder;
                        model.Format = extension.Replace(".", "");
                        model.UploadDate = DateTime.Now;
                        //信息写入数据库
                        BLL.TopicBLL bll = new BLL.TopicBLL();
                        Response.Write(string.Format(header, bll.AddPlate(model)));

                        //FileDeepProcess(string.Format("{0}{1}\\{2}", uploadPath, folderName, fileName));
                        //下面这句代码缺少的话，上传成功后上传队列的显示不会自动消失
                        //Response.Write(string.Format("{0}/{1}", folderName, fileName));
                    }
                    else
                    {
                        JsonResponse response = new JsonResponse();
                        response.success = false;
                        response.Message = "文件上传失败";
                        Response.Write(string.Format(header, JsonConvert.SerializeObject(response)));
                    }
                }

            }
            catch (Exception ex)
            {
                JsonResponse response = new JsonResponse();
                response.success = false;
                response.Message = ex.Message;
                Response.Write(JsonConvert.SerializeObject(response));
            }
        }
    }
}